﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ODS.Model;
using ODS.WebService.DeviceFunctions;
using ODS.Utils;
using ODS.Config;

namespace ODS
{
    public partial class AddNewDevicePage : PhoneApplicationPage
    {
        static bool isScanQRCodeDeviceIMEI = false;
        static bool isScanQRCodeSubscriptionCode = false;
        public AddNewDevicePage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (isScanQRCodeDeviceIMEI)
            {
                tb_deviceIMEI.MyText = App.TextQRCode;
            }
            else if (isScanQRCodeSubscriptionCode)
            {
                tb_subscriptionCode.MyText = App.TextQRCode;
            }

            isScanQRCodeDeviceIMEI = false;
            isScanQRCodeSubscriptionCode = false;
            
        }

        bool ValidateAddNewDeviceInput()
        {
            if (tb_deviceIMEI.MyText == "")
            {
                MessageBox.Show("Empty IMEI field, please try again.");
                return false;
            }

            if (tb_deviceIMEI.MyText.Length != 15)
            {
                MessageBox.Show("IMEI is 16 digit decimal, please try again.");
                return false;
            }

            if (tb_deviceName.MyText == "")
            {
                MessageBox.Show("Empty Vehicle field, please try again.");
                return false;
            }

            if (tb_devicePhoneNumber.MyText == "" )
            {
                MessageBox.Show("Empty Phone number field, please try again.");
                return false;
            }


            if (tb_devicePhoneNumber.MyText.Length > 15)
            {
                MessageBox.Show("Phone number:1-15 symbols, please try again.");
                return false;
            }

            if (tb_subscriptionCode.MyText == "")
            {
                MessageBox.Show("Empty SubscriptionCode field, please try again.");
                return false;
            }

            if (tb_subscriptionCode.MyText.Length < 32 && tb_subscriptionCode.MyText.Length > 64)
            {
                MessageBox.Show("SubscriptionCode strong length from 32 to 64 symbol, please try again.");
                return false;
            }

            return true;
        }

        private void btn_back_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.NavigationService.GoBack();
        }

        private void btn_scanQRCodeDeviceIMEI_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            isScanQRCodeDeviceIMEI = true;
            this.NavigationService.Navigate(new Uri("/ScanQRCodePage.xaml", UriKind.Relative));
        }

        private void btn_scanSubscriptionCode_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            isScanQRCodeSubscriptionCode = true;
            this.NavigationService.Navigate(new Uri("/ScanQRCodePage.xaml", UriKind.Relative));
        }

        private async void btn_active_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (ValidateAddNewDeviceInput())
            {
                DeviceModel deviceMD = new DeviceModel();
                deviceMD.DeviceIMEI = tb_deviceIMEI.MyText;
                deviceMD.DevicePhoneNumber = tb_devicePhoneNumber.MyText;
                deviceMD.DeviceSubscriptionCode = tb_subscriptionCode.MyText;
                deviceMD.VehicleName = tb_deviceName.MyText;

                InfoResult result = await  ActivateDeviceService.ActivateDevice(deviceMD);
                if (result.IsSuccess)
                {
                    if ((String)result.ReturnedObject == Constants.SUCCESS)
                    {
                        MessageBox.Show("Add new device successful.");

                        App.BackToMainPage(this);
                    }
                    else
                    {
                        MessageBox.Show((String)result.ReturnedObject);
                    }
                }
                else
                {
                    MessageBox.Show(result.Error);
                }
            }
        }
    }
}